package com.jinlejia.erp.ui.house;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.ListDropDownAdapter;
import com.jinlejia.erp.adapter.RentAdapter;
import com.jinlejia.erp.common.RecycleViewDivider;
import com.jinlejia.erp.databinding.ActivityAddInterestHouseBinding;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 选择兴趣房源
 */
public class AddInterestHouseActivity extends AppCompatActivity {


    private String headers[] = {"全部房源", "价格区间", "居室", "更多"};
    private String houseRes[] = {"不限", "查看核心房源", "关注房源", "限时房源", "钥匙房源", "热门房源", "维护房源"};
    private String prices[] = {"不限", "500元以下", "500-1000元", "1000-1500元", "1500-2000元", "2500-3000元", "3000元以上"};
    private String rooms[] = {"不限", "1室", "2室", "3室", "4室", "5室"};
    private List<View> popupViews = new ArrayList<>();
    private ListDropDownAdapter houseMenuAdapter;
    private ListDropDownAdapter roomMenuAdapter;
    private ListDropDownAdapter priceMenuAdapter;

    private RentAdapter adapter;

    private ActivityAddInterestHouseBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_add_interest_house);
        binding.btBack.setOnClickListener(this::onClick);
        binding.btSearch.setOnClickListener(this::onClick);
        binding.bootom.setOnClickListener(this::onClick);
        initview();
    }

    private void initview(){
        //房源菜单 menu
        final ListView houseView = new ListView(this);
        houseMenuAdapter = new ListDropDownAdapter(this, Arrays.asList(houseRes));
        houseView.setDivider(this.getResources().getDrawable(R.drawable.shape_h_line));
        houseView.setAdapter(houseMenuAdapter);
        //房源点击
        houseView.setOnItemClickListener((parent, view, position, id) -> {
            houseMenuAdapter.setCheckItem(position);
            binding.dropDownMenu.setTabText(position == 0 ? headers[0] : houseRes[position]);
            binding.dropDownMenu.closeMenu();
        });

        //价格区间
        final View priceAreaFooter = this.getLayoutInflater().inflate(R.layout.rent_price_area_footer, null);
        final ListView priceAreaView = new ListView(this);
        priceMenuAdapter = new ListDropDownAdapter(this, Arrays.asList(prices));
        priceAreaView.setDivider(this.getResources().getDrawable(R.drawable.shape_h_line));
        priceAreaView.addFooterView(priceAreaFooter);
        priceAreaView.setAdapter(priceMenuAdapter);
        //价格输入框
        final EditText leftPrice = priceAreaFooter.findViewById(R.id.et_left);
        final EditText rightPrice = priceAreaFooter.findViewById( R.id.et_right);
        //重置、确定按钮
        TextView cancel = priceAreaFooter.findViewById( R.id.bt_cancel);
        TextView confirm = priceAreaFooter.findViewById(R.id.bt_confirm);
        //价格列表点击
        priceAreaView.setOnItemClickListener((parent, view, position, id) -> {
            priceMenuAdapter.setCheckItem(position);
            binding.dropDownMenu.setTabText(position == 0 ? headers[1] : prices[position]);
            binding.dropDownMenu.closeMenu();
        });
        //点击重置
        cancel.setOnClickListener(v -> {
            leftPrice.setText("");
            rightPrice.setText("");
        });
        //确定按钮
        confirm.setOnClickListener(v -> {
            if (TextUtils.isEmpty(leftPrice.getText().toString())) {
                Toast.makeText(AddInterestHouseActivity.this, "请输入最低价格", Toast.LENGTH_SHORT).show();
            } else if (TextUtils.isEmpty(rightPrice.getText().toString())) {
                Toast.makeText(AddInterestHouseActivity.this, "请输入最高价格", Toast.LENGTH_SHORT).show();
            } else {
                binding.dropDownMenu.setTabText(leftPrice.getText().toString().concat("元-").concat(rightPrice.getText().toString()).concat("元"));
                binding.dropDownMenu.closeMenu();
            }
        });


        //  居室
        final ListView roomView = new ListView(this);
        roomMenuAdapter = new ListDropDownAdapter(this, Arrays.asList(rooms));
        roomView.setDivider(this.getResources().getDrawable(R.drawable.shape_h_line));
        roomView.setAdapter(roomMenuAdapter);
        //居室点击
        roomView.setOnItemClickListener((parent, view, position, id) -> {
            roomMenuAdapter.setCheckItem(position);
            binding.dropDownMenu.setTabText(position == 0 ? headers[2] : rooms[position]);
            binding.dropDownMenu.closeMenu();
        });


        //  更多
        final View moreView = new View(this);

        // 添加到view List
        popupViews.add(houseView);
        popupViews.add(priceAreaView);
        popupViews.add(roomView);
        popupViews.add(moreView);


        TextView contentView = new TextView(this);
        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
        contentView.setGravity(Gravity.CENTER);

        //init dropdownview
        binding.dropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView);
        binding.dropDownMenu.setOnMenuChangeListener(menuID -> {
            if( menuID == 6 ){
                //更多
                binding.dropDownMenu.closeMenu();
                startActivity(new Intent(AddInterestHouseActivity.this, MenuMoreActivity.class));
            }
        });

        binding.rvList.setHasFixedSize(true);
        binding.rvList.setLayoutManager(new LinearLayoutManager(this));
        ArrayList<Map<String,Object>> list = new ArrayList();
        for( int i = 0; i< 20;i++ ){
            list.add(new HashMap<>());
        }
        adapter = new RentAdapter(list,"CHECK");
        binding.rvList.setAdapter(adapter);
        binding.rvList.addItemDecoration(new RecycleViewDivider(
                this, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.colorDivider)));

    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_search:

                break;
            case R.id.bootom:
                Intent intent = new Intent(AddInterestHouseActivity.this,SelectedActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
